// 导入axios
import axios from 'axios'

const service = axios.create({
  // 配置请求的根路径
  baseURL: 'api',
  timeout: 10000
})

// 请求拦截器，发送请求前，把token先挂在到header中
service.interceptors.request.use(
  // Authorization 授权
  // 在最后必须return config
  (config) => {
    config.headers.Authorization = localStorage.getItem('token')
    return config
  },
  (error) => {
    console.log(error)
    return Promise.resolve({})
  }
)

service.interceptors.response.use(
  (response) => {
    // console.log('res', response)
    return response
  },
  (error) => {
    console.log(error)
    // 错误不往下抛
    return error.response
  }
)

export default service
